package com.juxing.service.liveCenter.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import com.google.gson.JsonParser;
import com.juxing.sdk.util.StringUtil;
import com.pili.Hub;
import com.pili.Stream;
import com.pili.Stream.SnapshotResponse;
import com.qiniu.Credentials;

public class PiliHelper implements InitializingBean {
	
	private Logger logger = LoggerFactory.getLogger(PiliHelper.class);

	private String accessKey;
	private String secretKey;
	private String hubName;

	private Credentials credentials;
	private Hub hub;

	@Override
	public void afterPropertiesSet() throws Exception {
		if(StringUtil.isEmpty(accessKey) || StringUtil.isEmpty(secretKey) || StringUtil.isEmpty(hubName)) {
			throw new RuntimeException("accessKey,secretKey,hubName不能为空");
		}
		logger.debug("accessKey:{}, secretKey:{}, hubName:{}", accessKey, secretKey, hubName);
		credentials = new Credentials(accessKey, secretKey);
		hub = new Hub(credentials, hubName);
	}

	/**
	 * 创建直播流
	 * 
	 * @return 七牛直播流json
	 */
	public String createStream() {
		logger.debug("createStream");
		int retry = 3;
		while(retry-- > 0) {
			try {
				Stream stream = hub.createStream(null, null, "dynamic");
				if(stream != null) {
					//创建成功
					return stream.toJsonString();
				}
			} catch (Exception e) {
				logger.error("error occurated while call createStream method.", e);
			}			
		}
		return null;
	}
	
	/**
	 * 获取一个流当前的snapshot
	 * @param streamJson
	 * @param name
	 * @return
	 */
	public String getSnapshotOfStream(String streamJson, String name) {
		Stream stream = this.getStreamFromJson(streamJson);
		int retry = 3;
		while(retry-- > 0) {
			try {
				SnapshotResponse response = stream.snapshot(name + ".jpg", "jpg");
				return response.getTargetUrl();
			} catch (Exception e) {
				logger.error("failed to get snapshot. streamJson=" + streamJson, e);
			}
		}
		return null;
	}
	
	/**
	 * 获取回放地址
	 * @param streamJson
	 * @param start in second, unix timestamp
	 * @param end in second, unix timestamp
	 * @return
	 */
	public String getHlsPlaybackUrl(String streamJson, long start, long end) {
		try {
			Stream stream = this.getStreamFromJson(streamJson);
			return stream.hlsPlaybackUrls(start, end).get(Stream.ORIGIN);
		} catch (Exception e) {
			logger.error("failed to get hlsPlaybackUrl. streamJson={}, start={}, end={}", streamJson, start, end);
			logger.error("failed to get hlsPlaybackUrl. ", e);
		}
		return null;
	}
	
	private Stream getStreamFromJson(String streamJson) {
		Stream stream = new Stream(new JsonParser().parse(streamJson).getAsJsonObject(), credentials);
		return stream;
	}
	
	
	public String getAccessKey() {
		return accessKey;
	}

	public void setAccessKey(String accessKey) {
		this.accessKey = accessKey;
	}

	public String getSecretKey() {
		return secretKey;
	}

	public void setSecretKey(String secretKey) {
		this.secretKey = secretKey;
	}

	public String getHubName() {
		return hubName;
	}

	public void setHubName(String hubName) {
		this.hubName = hubName;
	}

}
